package com.google.code.burner.pomodoro;

import java.util.Timer;

import javax.swing.JOptionPane;

public class SimplePomodoroTimer {

	private static final int TIME_POMODORO = 25;
	private static final int TIME_LARGEBREAK = 15;
	private static final int TIME_SMALLBREAK = 5;
	private static final long POLLING_INTERVAL = 250; // ms
	private boolean active = false;

	public static void main(String[] args) {
		new SimplePomodoroTimer();
	}

	private MainFrame frame;
	private Timer t;

	public SimplePomodoroTimer() {
		frame = new MainFrame(this);
		frame.setVisible(true);
		frame.setLabel(0);
		t = new Timer();
	}

	public boolean active() {
		return this.active;
	}

	public void startPomodoro() {
		t.schedule(new Task(this, TIME_POMODORO), 0, POLLING_INTERVAL);
		this.active = true;
	}

	public void setActive(boolean active) {
		if (!active) { // stopped
			t.cancel();
			t = new Timer();
			frame.setLabel(0);
		}
		this.active = active;
	}

	public void startSmallBreak() {
		this.active = true;
		t.schedule(new Task(this, TIME_SMALLBREAK), 0, POLLING_INTERVAL);
	}

	public void startLargeBreak() {
		this.active = true;
		t.schedule(new Task(this, TIME_LARGEBREAK), 0, POLLING_INTERVAL);
	}

	public void updateRemainingTimeLabel(long remaining) {
		frame.setLabel(remaining);
	}

	public void alert(String message) {
		JOptionPane.showMessageDialog(null, message, "Pomodoro Alarm", JOptionPane.OK_CANCEL_OPTION);
	}

}
